//
//  QJMineShouYiVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/26.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineShouYiVC.h"
#import "QJMineSYHeaderView.h"
#import "QJMineSKJLVC.h"
#import "QJMineSYLSVC.h"
#import "QJMineTXVC.h"
#import "QJMineHistoryFHVC.h"

@interface QJMineShouYiVC ()

@property (nonatomic, strong) UITableView *syTableView;
@property (nonatomic, strong) QJMineSYHeaderView *syHeaderView;
@property (nonatomic, strong) MineShouYiModel *model;
@end

@implementation QJMineShouYiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupShouYiVCAction];
}

- (void)setupShouYiVCAction{
    
    UserInfoModel *model = [QJUserInfo getCurrentUser];
    NSString *userTye = model.user_type;
    if ([userTye isEqualToString:@"3"]) {
        self.skjlBtn.hidden = NO;
    }else{
        self.skjlBtn.hidden = YES;
    }
    
//    // 测试
//    self.skjlBtn.hidden = NO;
//    // 测试结束
    
    self.syTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - 44) style:UITableViewStylePlain];
    [self.headerView addSubview:self.syTableView];
    self.syHeaderView = [[[NSBundle mainBundle]loadNibNamed:@"QJMineSYHeaderView" owner:self options:nil]firstObject];
    self.syHeaderView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 755);
    [self.syHeaderView setupHeaderUIAction];
    self.syTableView.tableHeaderView = self.syHeaderView;
    self.syTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(syDataRequestAction)];

    WEAKSELF
    self.syHeaderView.headerBtnClickBlockAction = ^(NSInteger tag) {
        switch (tag) {
            case 300:
            {
                //查看流水
                QJMineSYLSVC *VC = [[QJMineSYLSVC alloc]init];
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 301:
            {
                //去提现
                QJMineTXVC *VC = [[QJMineTXVC alloc]init];
                VC.ktxStr = self.model.ye;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 302:
            {
                //历史分红指数
                QJMineHistoryFHVC *VC = [[QJMineHistoryFHVC alloc]init];
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 303:
            {
                //如何获取1
                [MBPHUD showAlertWithTitle:@"泉界商家/泉界会员推荐注册的用户，在泉界商城产生消费，即可获得金元宝，产生的收益可提现！" duration:3.0];
            }
                break;
            case 304:
            {
                //如何获取2
                [MBPHUD showAlertWithTitle:@"泉界商家/泉界会员，在泉界商城自己消费，即可获得银元宝，产生的收益可用于消费！" duration:3.0];
            }
                break;
                
            default:
                break;
        }
    };
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavAlpha:YES];
    self.navigationController.navigationBarHidden = YES;
    [self syDataRequestAction];
    if (IOS_VERSION < 11.0) {
        self.navTopHeight.constant = 84;
    }else{
        self.navTopHeight.constant = 64;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavAlpha:NO];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark -- 数据请求方法
- (void)syDataRequestAction{
    [self.syTableView.mj_header endRefreshing];

    WEAKSELF
    [WWNetRequest fetchMyEarningsWithParameters:nil success:^(id response) {
        NSLog(@"我的收益 response = %@", response);
        if ([response isKindOfClass:[NSDictionary class]]) {
            NSDictionary *newDict = response[@"data"][@"new"];
            NSDictionary *oldDict = response[@"data"][@"old"];
            NSDictionary *totalDict = response[@"data"][@"total"];

            // 这里的类型 NSString跟 NSNumber 不一样, 都用字符串算了
            MineShouYiModel *model = [[MineShouYiModel alloc] init];
            // 总收益 ;
            model.totalFee = [NSString stringWithFormat:@"%@", totalDict[@"total_business"]];
            // 冻结金额
            model.dj = [NSString stringWithFormat:@"%@", totalDict[@"frozen_business"]];
            // 已提现
            model.ytx = [NSString stringWithFormat:@"%@", totalDict[@"raise_business"]];
            // 可提现金额
            model.ye = [NSString stringWithFormat:@"%@", totalDict[@"balance_business"]];
            // 金元宝 可用
            model.gold_ky = [NSString stringWithFormat:@"%@", totalDict[@"balance_gold"]];
            // 金元宝 待发 已冻结
            model.silver_df = [NSString stringWithFormat:@"%@", totalDict[@"frozen_gold"]];
            // 金元宝 已扣除
            model.gold_ykc = [NSString stringWithFormat:@"%@", totalDict[@"raise_gold"]];
            // 金元宝  昨日收益
            model.gold_sy = @"0.00";
            // 银元宝 可用
            model.silver_ky = [NSString stringWithFormat:@"%@", totalDict[@"balance_silver"]];
            // 银元宝  待发
            model.silver_df = [NSString stringWithFormat:@"%@", totalDict[@"frozen_silver"]];
            // 银元宝 已消费
            model.silver_yxf = [NSString stringWithFormat:@"%@", totalDict[@"raise_silver"]];
            // 银元宝  昨日收益
            model.silver_zr_sy = @"0.00";
            // 银元宝 总收益
            model.silver_total_sy = [NSString stringWithFormat:@"%@", totalDict[@"total_silver"]];
            // 昨日分红指数
            model.zrsy = @"0";
            //
            /*
             金元宝/银元宝 的昨日收益,都是 yestoday,offline 里还有值
             offline + online
             yestoday =         {
             offline = "<null>";
             online = "<null>";
             };
             */
            // 不用字典转模型了,现在改为3个表
//            [[MineShouYiModel alloc]initWithDictionary:response error:nil];
            weakSelf.model = model;
            [weakSelf.syHeaderView headerUpdateWithModel:model];
        }
    } failure:^(NSDictionary *error) {
        NSLog(@"我的收益 error = %@", error);

    }];
}
- (IBAction)backBtnClickAction:(UIButton *)sender {
    KPopViewControllerAction;
}
//收款记录
- (IBAction)skBtnClockAction:(UIButton *)sender {
    QJMineSKJLVC *VC = [[QJMineSKJLVC alloc]init];
    KPushViewControllerAction(VC);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
